// var cookie = document.cookie;
// if (!(cookie && /username/.test(cookie))) {
//   window.location = './login';
// }
// $(window).on('scroll.elasticity',function (e){e.preventDefault();}).on('touchmove.elasticity',function(e){e.preventDefault();});
window.addEventListener('scroll.elasticity', function (e) {
    e.preventDefault();
});
window.addEventListener('touchmove.elasticity', function (e) {
    e.preventDefault();
})
var socket = io('myblog.liuwenxi.me');
var val = document.querySelector('.chat-info');
var sub = document.querySelector('.submit-info');
var chat_room = document.querySelector('.chat-room');
var style = document.querySelector('html').style;
var font_size = style['font-size'].match(/[0-9]*/)[0];
var main = document.querySelector('main');
var chat_room_height = window.getComputedStyle ? window.getComputedStyle(main,null).height : main.currentStyle.height;
chat_room_height = chat_room_height.match(/[0-9]*/)[0];
chat_room_height -= font_size;
chat_room.style.height = chat_room_height + 'px';
sub.addEventListener('click', function (e) {
    socket.emit('broadcast', {
        info: val.value,
        cookie_info: document.cookie
    })
    val.value = null;
});
socket.on('res_broadcast', function (data) {
  // chat_room.innerHTML += "<li>" + data.res + "</li>";
  var date = new Date(data.time);
  var hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
  var minute = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
  var second = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  var time = hour + ":" + minute + ":" + second;
  chat_room.innerHTML +=  "<div class='chat-info-other'><div class='user-info'><span class='username'>__username__</span><span class='time'>__time__</span></div><div class='chat-content'>__chat_content__</div></div>".replace(/__chat_content__/, data.res).replace(/__username__/, data.username).replace(/__time__/, time);
  chat_room.scrollTop = chat_room.scrollHeight;
  console.log(data);
});
socket.on('confirm_broadcast', function (data) {
    if (data.res === 'success') {
        var date = new Date(data.time);
        var hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
        var minute = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
        var second = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
        var time = hour + ":" + minute + ":" + second;
        chat_room.innerHTML += "<div class='chat-info-self'><div class='user-info'><div class='info-con'><span class='username'>__username__</span><span class='time'>__time__</span></div></div><div class='chat-content'>__chat_content__</div></div>".replace(/__chat_content__/, data.info).replace(/__username__/, data.username).replace(/__time__/, time);
        chat_room.scrollTop = chat_room.scrollHeight;
        console.log(data)
    }
});
